package com.example.healkep;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.healkep.bean.ItemBean;

import org.json.JSONArray;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class FoodNewsDetailActivity extends AppCompatActivity {

    private TextView mTvTitle;
    private TextView mTvContent;
    private ImageView mIvImage;

    private ItemBean mNewsBean;

    String username = LoginActivity.getUser();
    int foodid;
    String name;
    String ingredients;
    String accessories;
    String introduction;
    String label;
    String cooking;
    String favor;
    String functions;
    String target;
    String disease;
    String constitution;
    String cuisine;
    String snack;
    String type;
    String kitchenware;
    String occasion;

    int responsenum;
    String responseData;
    JSONArray jsonArray;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                Switch aSwitch = (Switch) findViewById(R.id.switch1);
                if (responsenum > 0) aSwitch.setChecked(true); else aSwitch.setChecked(false);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (getSupportActionBar() != null){
            getSupportActionBar().hide();
        }   //隐藏标题栏
        super.onCreate(savedInstanceState);
        setContentView(R.layout.food_activity_news_detail);
        name = (String) getIntent().getSerializableExtra("name");
        ingredients = (String) getIntent().getSerializableExtra("ingredients");
        accessories = (String) getIntent().getSerializableExtra("accessories");
        introduction = (String) getIntent().getSerializableExtra("introduction");
        label = (String) getIntent().getSerializableExtra("label");
        cooking = (String) getIntent().getSerializableExtra("cooking");
        favor = (String) getIntent().getSerializableExtra("favor");
        functions = (String) getIntent().getSerializableExtra("functions");
        target = (String) getIntent().getSerializableExtra("target");
        disease = (String) getIntent().getSerializableExtra("disease");
        constitution = (String) getIntent().getSerializableExtra("constitution");
        cuisine = (String) getIntent().getSerializableExtra("cuisine");
        snack = (String) getIntent().getSerializableExtra("snack");
        type = (String) getIntent().getSerializableExtra("type");
        kitchenware = (String) getIntent().getSerializableExtra("kitchenware");
        occasion = (String) getIntent().getSerializableExtra("occasion");
        foodid = getIntent().getIntExtra("id",1);

        initView();
        initData();
        initEvent();

        Switch aSwitch = (Switch) findViewById(R.id.switch1);

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    FormBody.Builder params = new FormBody.Builder(); //建立
                    params.add("username", username);
                    params.add("foodid", String.valueOf(foodid));
                    OkHttpClient client = new OkHttpClient();
                    Request request;
                    request = new Request.Builder()
                            .url(LoginActivity.url+"/food/searchfavor")
                            .post(params.build())
                            .build();
                    Response response = client.newCall(request).execute();
                    responseData = response.body().string();  //将response转为string类型的responsedata
                    jsonArray = new JSONArray(responseData); //转换为json数组
                    responsenum = jsonArray.length();

                    Message msg = new Message();
                    msg.what = 1;
                    handler.sendMessage(msg);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();

        aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                FormBody.Builder params = new FormBody.Builder(); //建立
                                params.add("username", username);
                                params.add("foodid", String.valueOf(foodid));
                                OkHttpClient client = new OkHttpClient();
                                Request request = null;
                                request = new Request.Builder()
                                        .url(LoginActivity.url+"/food/addfavor")
                                        .post(params.build())
                                        .build();
                                Response response = client.newCall(request).execute();
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(FoodNewsDetailActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                                    }
                                });
                            }catch (Exception e){
                                e.printStackTrace();
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(FoodNewsDetailActivity.this,"收藏失败",Toast.LENGTH_SHORT).show();
                                    }
                                });
                            }
                        }
                    }).start();
                }
                else {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                FormBody.Builder params = new FormBody.Builder(); //建立
                                params.add("username", username);
                                params.add("foodid", String.valueOf(foodid));
                                OkHttpClient client = new OkHttpClient();
                                Request request = null;
                                request = new Request.Builder()
                                        .url(LoginActivity.url+"/food/deletefavor")
                                        .post(params.build())
                                        .build();
                                Response response = client.newCall(request).execute();
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(FoodNewsDetailActivity.this, "取消收藏", Toast.LENGTH_SHORT).show();
                                    }
                                });
                            }catch (Exception e){
                                e.printStackTrace();
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(FoodNewsDetailActivity.this,"取消收藏失败",Toast.LENGTH_SHORT).show();
                                    }
                                });
                            }
                        }
                    }).start();
                }
            }
        });
    }

    private void initView() {

        mIvImage = findViewById(R.id.iv_img);
        mTvContent = findViewById(R.id.tv_content);
        mTvTitle = findViewById(R.id.tv_title);

    }
    private void initData() {

        Intent intent = getIntent();
        mNewsBean = (ItemBean) intent.getSerializableExtra("item");

    }
    private void initEvent() {
        if (mNewsBean != null) {
            mTvTitle.setText(mNewsBean.getTitle());
            mTvContent.setText("id:" + getIntent().getIntExtra("id",1) +
                    "\n食谱: " + name +
                    "\n主料: " + ingredients +
                    "\n辅料: " + accessories +
                    "\n简介: " + introduction +
                    "\n标签: " + label +
                    "\n烹饪: " + cooking +
                    "\n口味: " + favor +
                    "\n功能: " + functions +
                    "\n适用人群: " + target +
                    "\n适用疾病: " + disease +
                    "\n体质: " + constitution +
                    "\n菜系: " + cuisine +
                    "\n小吃: " + snack +
                    "\n类型: " + type +
                    "\n厨具: " + kitchenware +
                    "\n背景: " + occasion);
            mIvImage.setImageResource(mNewsBean.getImgResId());
        }
    }

}